home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / netlib / getopt.c < prev    next >
C/C++ Source or Header  |  1993-08-12  |  2KB  |  82 lines

  1. /*
  2. **    @(#)getopt.c    2.5 (smail) 9/15/87
  3. */
  4.  
  5. /*
  6.  * Here's something you've all been waiting for:  the AT&T public domain
  7.  * source for getopt(3).  It is the code which was given out at the 1985
  8.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  9.  * directly from AT&T.  The people there assure me that it is indeed
  10.  * in the public domain.
  11.  * 
  12.  * There is no manual page.  That is because the one they gave out at
  13.  * UNIFORUM was slightly different from the current System V Release 2
  14.  * manual page.  The difference apparently involved a note about the
  15.  * famous rules 5 and 6, recommending using white space between an option
  16.  * and its first argument, and not grouping options that have arguments.
  17.  * Getopt itself is currently lenient about both of these things White
  18.  * space is allowed, but not mandatory, and the last option in a group can
  19.  * have an argument.  That particular version of the man page evidently
  20.  * has no official existence, and my source at AT&T did not send a copy.
  21.  * The current SVR2 man page reflects the actual behavor of this getopt.
  22.  * However, I am not about to post a copy of anything licensed by AT&T.
  23.  */
  24.  
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <stdio.h>
  28.  
  29. /*LINTLIBRARY*/
  30. #define EOF    (-1)
  31. #define ERR(s, c)\
  32.   if(opterr) { fprintf(stderr, "%s%s%lc\n", argv[0], s, c); }
  33.  
  34. int    opterr = 1;
  35. int    optind = 1;
  36. int    optopt;
  37. char    *optarg;
  38.  
  39. int
  40. getopt(int argc, char **argv, char *opts)
  41. {
  42.     static int sp = 1;
  43.     register int c;
  44.     register char *cp;
  45.  
  46.     if(sp == 1)
  47.         if(optind >= argc ||
  48.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  49.             return(EOF);
  50.         else if(strcmp(argv[optind], "--") == NULL) {
  51.             optind++;
  52.             return(EOF);
  53.         }
  54.     optopt = c = argv[optind][sp];
  55.     if(c == ':' || (cp=index(opts, c)) == NULL) {
  56.         ERR(": illegal option -- ", c);
  57.         if(argv[optind][++sp] == '\0') {
  58.             optind++;
  59.             sp = 1;
  60.         }
  61.         return('?');
  62.     }
  63.     if(*++cp == ':') {
  64.         if(argv[optind][sp+1] != '\0')
  65.             optarg = &argv[optind++][sp+1];
  66.         else if(++optind >= argc) {
  67.             ERR(": option requires an argument -- ", c);
  68.             sp = 1;
  69.             return('?');
  70.         } else
  71.             optarg = argv[optind++];
  72.         sp = 1;
  73.     } else {
  74.         if(argv[optind][++sp] == '\0') {
  75.             sp = 1;
  76.             optind++;
  77.         }
  78.         optarg = NULL;
  79.     }
  80.     return(c);
  81. }
  82.